package com.wing.member.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.PageResult;
import com.wing.member.model.entity.MemberLoginLog;
import com.wing.member.service.MemberLoginLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Api(value = "Admin-会员登录记录管理", tags = "#/member/memberLoginLog")
@RestController("memberLoginLogController")
@RequestMapping("member/admin/memberLoginLog")
public class MemberLoginLogController {

    @Autowired
    private MemberLoginLogService memberLoginLogService;

    @ApiOperation(value = "查询所有会员登录记录", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "memberUsername", value = "用户名", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "ipAddress", value = "IP地址", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<MemberLoginLog> list(
        Integer page
        ,Integer limit
        ,String sort
        ,String order
        ,String memberUsername
        ,String ipAddress
    ){

        Page<MemberLoginLog> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", StrUtil.toUnderlineCase(sort));
            params.put("order", StrUtil.toUnderlineCase(order));
        }

        //搜索
        if (StringUtils.isNotBlank(memberUsername)) {
            params.put("memberUsername", memberUsername);
        }
        if (ipAddress != null) {
            params.put("ipAddress", ipAddress);
        }

        IPage<MemberLoginLog> list = memberLoginLogService.pageList(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

}
